
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;

/**
 * Set:与List的区别，不需要保存重复数据
 * HashSet：无序存储
 * LinkedHashSet：基于链表，按添加顺序存储
 */
class Student implements Comparable<Student>{

    public Student(String id, String name) {
        this.id = id;
        this.name = name;
    }

    String id;
    String name;
    @Override
    public int compareTo(Student o) {
        return id.compareTo(o.id);
    }

    @Override
    public String toString() {
        return "Student{" + "id=" + id + ", name=" + name + '}';
    }
    
}
public class SetTest {
    public static void HashSetTest(){
        Set<String> set=new HashSet<String>();
        set.add("18计科1班");
        set.add("18计科2班");
        set.add("18软工1班");
        set.add("18软工2班");
        set.add("18软工3班");
        System.out.println(set);
    }
    public static void LinkedHashSetTest(){
        Set<String> set=new LinkedHashSet<String>();
        set.add("18计科1班");
        set.add("18计科2班");
        set.add("18软工1班");
        set.add("18软工2班");
        set.add("18软工3班");
        System.out.println(set);
    }
    
    public static void TreeSetTest(){
        Set<String> set=new TreeSet<String>();
        set.add("18软工1班");
        set.add("18软工2班");
        set.add("18软工3班");
        set.add("18计科1班");
        set.add("18计科2班");
        System.out.println(set);
        
        System.out.println("------自定义类实现Comparable接口，添加入TreeSet------");
        Set<Student> set2=new TreeSet<Student>();
        set2.add(new Student("110","张三"));
        set2.add(new Student("111","李四"));
        set2.add(new Student("109","王五"));
        set2.add(new Student("112","赵六"));
        System.out.println(set2);
    }
    public static void main(String[] args){
        System.out.println("------HashSet测试：无序存储------");
        HashSetTest();
        
        System.out.println("\n\n------LinkedHashSet测试：按添加顺序存储------");
        LinkedHashSetTest();
        
        System.out.println("\n\n------TreeSet测试：按Comparable接口实现顺序存储------");
        TreeSetTest();
     }
}
